package ext.tianma.part.resolution;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import ext.com.iba.IBAUtil;
import ext.tianma.part.forms.TMTypeUtil;
import wt.enterprise.Master;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.part.WTPartConfigSpec;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartStandardConfigSpec;
import wt.part.WTPartUsageLink;
import wt.pom.PersistenceException;
import wt.util.WTException;
import wt.vc.Iterated;
import wt.vc.VersionControlHelper;
import wt.vc.config.ConfigSpec;
import wt.vc.views.ViewHelper;

public class UpdateIBAForChildPart {

	public static void getPartIBAForPPart(WTPart part) {
		try {
				List<WTPart> partList;
				List<WTPart> partListToIBA = new ArrayList<WTPart>();
				Hashtable<String, String> values = new Hashtable<String, String>();
				System.out.println("ss===>>"+part.getNumber());
				partList = getBOMList(part, null);
				System.out.println("partList=====>>"+partList);
				int i = 1;
				// 如果当前料为半成品
				String jcd = "";
				String LCDpbs = "";
				for (WTPart parts : partList) {
					System.out.println("qq===>>"+parts.getNumber());
					if (TMTypeUtil.isSemiFinishedGood(parts)) {
						
						partListToIBA.add(parts);
						System.out.println("parts=====第" + i + "个部件" + parts.getNumber());
						if (parts.getNumber().startsWith("T1") || parts.getNumber().startsWith("L1")) {
							jcd = (String) IBAUtil.getIBAValue(parts, "Integration");// 集成度
							LCDpbs = (String) IBAUtil.getIBAValue(parts, "LCDTypesetting");// LCD排版数
							
						}
						
						i++;
					}
				}
				for (WTPart partIBA : partListToIBA) {
					System.out.println("jcd===>>"+jcd);
					System.out.println("partIBA===>>"+partIBA.getNumber());
					 addToHashtable(values,"Integration", LCDpbs);
					 addToHashtable(values,"LCDTypesetting", jcd);
					IBAUtil.setSoftTypeIBAValues(partIBA, values);
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 遍历BOM，递归方法
	public static List<WTPart> getBOMList(WTPart part, ConfigSpec cs1) throws Exception {
		List partList = new ArrayList();
		if (part == null)
			return partList;

		// partList.add(part);

		// ConfigSpec cs = getCurrentConfigSpec(cs1);
		ConfigSpec cs = null;
		if (cs1 == null)
			cs = WTPartConfigSpec.newWTPartConfigSpec(WTPartStandardConfigSpec
					.newWTPartStandardConfigSpec(ViewHelper.service.getView(part.getViewName()), null));
		else
			cs = cs1;

		QueryResult subNodes = WTPartHelper.service.getUsesWTParts(part, cs);
		Vector v = subNodes.getObjectVectorIfc().getVector();
		for (int i = 0; i < v.size(); i++) {
			Persistable subnode[] = (Persistable[]) v.get(i);
			WTPart childPart = (WTPart) subnode[1];
			// WTPartUsageLink link = (WTPartUsageLink) subnode[0];

			getBOMList(childPart, cs, partList);
		}

		// Collections.sort(ret, new LineComparator());
		return partList;
	}

	private static void getBOMList(WTPart part, ConfigSpec cs, List partList) throws Exception {
		if (part == null || cs == null)
			return;
		String number = part.getNumber();

		if (!partList.contains(part)) {
			partList.add(part);
		}

		QueryResult subNodes = WTPartHelper.service.getUsesWTParts(part, cs);
		Vector v = subNodes.getObjectVectorIfc().getVector();
		for (int i = 0; i < v.size(); i++) {
			Persistable subnode[] = (Persistable[]) v.get(i);
			WTPart childPart = (WTPart) subnode[1];
			// WTPartUsageLink childLink = (WTPartUsageLink) subnode[0];
			getBOMList(childPart, cs, partList);
		}
	}

	/**
	 * 获取BOM结构信息 修改半成品属性
	 * 
	 * @param part
	 *            物料对象
	 * @param producePartsList
	 *            结构下所有物料集合
	 * @return
	 * @throws WTException
	 * @throws RemoteException
	 */
	public static List<WTPart> getChildPartByFPart(WTPart wtPart, List<WTPart> producePartsList)
			throws WTException, RemoteException {
		QueryResult result1 = PersistenceHelper.manager.navigate(wtPart, WTPartUsageLink.USES_ROLE,
				WTPartUsageLink.class, false);
		ArrayList arrayListVec = new ArrayList(result1.getObjectVectorIfc().getVector());
		Hashtable<String, String> values = new Hashtable<String, String>();
		for (int i = 0; i < arrayListVec.size(); i++) {
			WTPartUsageLink link = (WTPartUsageLink) arrayListVec.get(i);
			WTPartMaster childmaster = (WTPartMaster) link.getUses();
			WTPart childParts = (WTPart) findLatestedIterationByMaster(childmaster);
			System.out.println("partNumber===>>" + childParts.getNumber());
			if (childParts.getNumber().startsWith("T1") || childParts.getNumber().startsWith("L1")) {
				String jcd = (String) IBAUtil.getIBAValue(childParts, "Integration");// 集成度
				String LCDpbs = (String) IBAUtil.getIBAValue(childParts, "LCDTypesetting");// LCD排版数
				 addToHashtable(values,"Integration", LCDpbs);
				 addToHashtable(values,"LCDTypesetting", jcd);
				IBAUtil.setSoftTypeIBAValues(wtPart, values);

			}
			getChildPartByFPart(childParts, producePartsList);
		}
		return producePartsList;
	}

	/**
	 * 获取最新版本的对象
	 * 
	 * @param master
	 * @return
	 * @throws PersistenceException
	 * @throws WTException
	 */
	public static Iterated findLatestedIterationByMaster(Master master) throws PersistenceException, WTException {
		QueryResult qr = VersionControlHelper.service.allVersionsOf(master);
		return (Iterated) qr.nextElement();
	}
	
	
	
	/**
	 * 实现Hashtable添加数据时不会出现空指针异常
	 * 
	 * @param hashtable
	 * @param key
	 * @param value
	 * @return
	 */
	private static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
			String value) {
		if (key == null && hashtable != null) {
			return hashtable;
		}

		if (value == null && hashtable != null) {
			return hashtable;
		}

		if (key == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (value == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (hashtable == null) {
			hashtable = new Hashtable<String, String>();
		}
		hashtable.put(key, value);

		return hashtable;
	}


}
